# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.itemAnncDao import ItemAnncDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertItemAnnc', methods=['POST'])
def insert_item_annc():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'anncTitle': request.json.get('anncTitle', ''),
        'deptId': request.json.get('deptId', 0),
        'anncPubTime': request.json.get('anncPubTime', ''),
        'anncUrl': request.json.get('anncUrl', ''),
        'anncStatus': request.json.get('anncStatus', 0),
        'anncRemark': request.json.get('anncRemark', '')

    }

    info = ItemAnncDao().insertItemAnnc(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findItemAnncById', methods=['POST'])
def find_item_annc_by_id():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    anncId = request.json.get('anncId', '')
    info = ItemAnncDao().findItemAnncById(anncId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findItemAnncByCondition', methods=['POST'])
def find_item_annc_by_condition():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    deptId = request.json.get('deptId', '')
    info = ItemAnncDao().findItemAnncByCondition(deptId=deptId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteItemAnncByIds', methods=['POST'])
def delete_item_annc_by_ids():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = ItemAnncDao().deleteItemAnnc(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateItemAnncById', methods=['POST'])
def update_item_annc_by_id():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        anncId = request.json['anncId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = ItemAnncDao().selectItemAnnc(anncId)
    info = {
        'anncId': request.json['anncId'],
        'anncTitle': request.json.get('anncTitle', oldInfo[1]),
        'deptId': request.json.get('deptId', oldInfo[2]),
        'anncPubTime': request.json.get('anncPubTime', oldInfo[3]),
        'anncUrl': request.json.get('anncUrl', oldInfo[4]),
        'anncStatus': request.json.get('anncStatus', oldInfo[5]),
        'anncRemark': request.json.get('anncRemark', oldInfo[6])
    }

    info = ItemAnncDao().updateItemAnnc(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
